package com.example.opengldemo._09_OBJ;

import android.content.Context;
import android.opengl.GLES30;
import android.opengl.GLSurfaceView;
import android.util.Log;
import android.view.MotionEvent;

import com.example.opengldemo.R;
import com.example.opengldemo.util.LoadUtil;
import com.example.opengldemo.util.MatrixState;
import com.example.opengldemo.util.TextureUtil;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class MyObjFileView extends  GLSurfaceView implements GLSurfaceView.Renderer {
    ObjDemo objDemo;

    public MyObjFileView(Context context) {
        super(context);
        initConfig() ;
    }
    private float mPreviousY;
    private float mPreviousX;
    private final float TOUCH_SCALE_FACTOR = 180.0f/360;
    float cAngle=0;
    float cr=24;
    @Override
    public boolean onTouchEvent(MotionEvent e)
    {
        float y = e.getY();
        float x = e.getX();
        switch (e.getAction()) {
            case MotionEvent.ACTION_MOVE:
                float dy = y - mPreviousY;
                float dx = x - mPreviousX;
                cAngle+=dx * TOUCH_SCALE_FACTOR;
                MatrixState.cameraLocation[0]=(float) (Math.sin(Math.toRadians(cAngle))*cr);
                MatrixState.cameraLocation[2]=(float) (Math.cos(Math.toRadians(cAngle))*cr);
                MatrixState.cameraLocation[1]+=dy/10.0f;
        }
        mPreviousY = y;
        mPreviousX = x;
        return true;
    }
    private void initConfig() {
        this.setEGLContextClientVersion(3);
        this.setRenderer(this);
        this.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
    }

    @Override
    public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
        GLES30.glClearColor(0.5f  , 0.5f , 0.5f , 1.0f);
        GLES30.glEnable(GLES30.GL_DEPTH_TEST);
        GLES30.glDisable(GLES30.GL_CULL_FACE);
        objDemo = LoadUtil.getLoadObj(this , "nine/ch_t.obj") ;
        MatrixState.setInitStack();
    }
    int texture2 ;
    int texture3 ;
    @Override
    public void onSurfaceChanged(GL10 gl10, int i, int i1) {
        GLES30.glViewport( 0 , 0 , i , i1);
        float ratio= (float) i / i1;
        MatrixState.setProjectFrustum(-ratio, ratio, -1, 1, 4f, 1000);
        MatrixState.setCamera(0,0,7.2f,0f,0f,0f,0f,1.0f,0.0f);
        //GLES30.glEnable(GLES30.GL_CULL_FACE);
        MatrixState.setLightLocation(100,5,0);
        texture2 = TextureUtil.getTextureId(this , R.drawable.was1 , null , false) ;
        texture3 =  TextureUtil.getTextureId(this , R.drawable.was2 , null, false) ;
    }

    @Override
    public void onDrawFrame(GL10 gl10) {
        GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
        MatrixState.setCamera(MatrixState.cameraLocation[0],MatrixState.cameraLocation[1],MatrixState.cameraLocation[2],0f,0f,0f,0f,1.0f,0.0f);
        MatrixState.pushMatrix();
        MatrixState.translate(0 , 0 , 0.9f);
        if(objDemo != null){
            objDemo.drawSelf( texture2 , texture3 , 0);
        }

        MatrixState.popMatrix();
    }
}
